package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.components.element.VectorElement;
import ohos.agp.utils.Color;

import java.util.Map;

import static com.ohos.carbon.ResourceTable.*;
import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class BottomNavigationSlice extends AbilitySlice implements Component.ClickedListener {
    private RadioButton radioButton_one, radioButton_three, radioButton_two, radioButton_two1, radioButton_two2, radioButton_two3, radioButton_three1, radioButton_three2, radioButton_three3, radioButton_three4, radioButton_three5;
    private RadioContainer radioContainer, radioContainer3;
    private Image back_button;
    private Text text_title, ohos1, ohos2, calendar1, calendar2, comment1, comment2;
    private Text text1, text2, text3, text4, text5;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_bottom_navigation);
        initView();
        initRadiobutton();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("BottomNavigationView")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("BottomNavigationView")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("BottomNavigationView", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("BottomNavigationView", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("BottomNavigationView", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("BottomNavigationView");
        back_button.setClickedListener(this);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        back_button.setClickedListener(component -> onBackPressed());
    }

    private void initRadiobutton() {
        radioContainer = (RadioContainer) findComponentById(ResourceTable.Id_radiocontainer);
        ohos1 = (Text) findComponentById(Id_ohos1);
        calendar1 = (Text) findComponentById(Id_calendar1);
        comment1 = (Text) findComponentById(Id_comment1);
        radioButton_one = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_one);
        radioButton_two = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_two);
        radioButton_three = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_three);
        radioButton_one.setButtonElement(null);
        radioButton_two.setButtonElement(null);
        radioButton_three.setButtonElement(null);

        VectorElement radiobutton_element_ohos = new VectorElement(getContext(), ResourceTable.Graphic_ic_star_black_24dp);
        VectorElement radiobutton_element_today = new VectorElement(getContext(), ResourceTable.Graphic_ic_today_24px);
        VectorElement radiobutton_element_comment = new VectorElement(getContext(), ResourceTable.Graphic_ic_comment_black_24dp);
        radioButton_one.setBackground(radiobutton_element_ohos);
        radioButton_two.setBackground(radiobutton_element_today);
        radioButton_three.setBackground(radiobutton_element_comment);

        radioContainer.setMarkChangedListener((radioContainer, i) -> {
            if (radioButton_one.isChecked()) {
                ohos1.setTextColor(new Color(0xFFFFB90F));
                radioButton_one.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_ic_star_yellow_24dp));
            } else {
                ohos1.setTextColor(Color.BLACK);
                radioButton_one.setBackground(radiobutton_element_ohos);
            }
            if (radioButton_two.isChecked()) {
                calendar1.setTextColor(new Color(0xFFFFB90F));
                radioButton_two.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_today_yellow_24px));
            } else {
                calendar1.setTextColor(Color.BLACK);
                radioButton_two.setBackground(radiobutton_element_today);
            }
            if (radioButton_three.isChecked()) {
                comment1.setTextColor(new Color(0xFFFFB90F));
                radioButton_three.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_comment_yellow_24dp));
            } else {
                comment1.setTextColor(Color.BLACK);
                radioButton_three.setBackground(radiobutton_element_comment);
            }
        });

        radioContainer = (RadioContainer) findComponentById(ResourceTable.Id_radiocontainer_two);
        ohos2 = (Text) findComponentById(Id_ohos2);
        calendar2 = (Text) findComponentById(Id_calendar2);
        comment2 = (Text) findComponentById(Id_comment2);
        radioButton_two1 = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_two1);
        radioButton_two2 = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_two2);
        radioButton_two3 = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_two3);
        radioButton_two1.setButtonElement(null);
        radioButton_two2.setButtonElement(null);
        radioButton_two3.setButtonElement(null);

        VectorElement radiobutton_element_ohos_two = new VectorElement(getContext(), ResourceTable.Graphic_ic_star_black_24dp);
        VectorElement radiobutton_element_today_two = new VectorElement(getContext(), ResourceTable.Graphic_ic_today_24px);
        VectorElement radiobutton_element_comment_two = new VectorElement(getContext(), ResourceTable.Graphic_ic_comment_black_24dp);
        radioButton_two1.setBackground(radiobutton_element_ohos_two);
        radioButton_two2.setBackground(radiobutton_element_today_two);
        radioButton_two3.setBackground(radiobutton_element_comment_two);

        radioContainer.setMarkChangedListener((radioContainer, i) -> {
            if (radioButton_two1.isChecked()) {
                ohos2.setTextColor(new Color(0xFFFFB90F));
                radioButton_two1.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_ic_star_yellow_24dp));
            } else {
                ohos2.setTextColor(Color.BLACK);
                radioButton_two1.setBackground(radiobutton_element_ohos_two);
            }
            if (radioButton_two2.isChecked()) {
                calendar2.setTextColor(new Color(0xFFFFB90F));
                radioButton_two2.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_today_yellow_24px));
            } else {
                calendar2.setTextColor(Color.BLACK);
                radioButton_two2.setBackground(radiobutton_element_today_two);
            }
            if (radioButton_two3.isChecked()) {
                comment2.setTextColor(new Color(0xFFFFB90F));
                radioButton_two3.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_comment_yellow_24dp));
            } else {
                comment2.setTextColor(Color.BLACK);
                radioButton_two3.setBackground(radiobutton_element_comment_two);
            }
        });

        radioContainer3 = (RadioContainer) findComponentById(ResourceTable.Id_radioContainer_three);
        text1 = (Text) findComponentById(Id_radioButton_1);
        text2 = (Text) findComponentById(Id_radioButton_2);
        text3 = (Text) findComponentById(Id_radioButton_3);
        text4 = (Text) findComponentById(Id_radioButton_4);
        text5 = (Text) findComponentById(Id_radioButton_5);
        radioButton_three1 = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_three1);
        radioButton_three2 = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_three2);
        radioButton_three3 = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_three3);
        radioButton_three4 = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_three4);
        radioButton_three5 = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_three5);
        radioButton_three1.setButtonElement(null);
        radioButton_three2.setButtonElement(null);
        radioButton_three3.setButtonElement(null);
        radioButton_three4.setButtonElement(null);
        radioButton_three5.setButtonElement(null);

        VectorElement radiobutton_element_message = new VectorElement(getContext(), ResourceTable.Graphic_ic_email_black_24dp);
        radioButton_three4.setBackground(radiobutton_element_message);

        radioContainer3.setMarkChangedListener((radioContainer, i) -> {
            if (radioButton_three1.isChecked()) {
                text1.setTextColor(new Color(0xFFFFB90F));
            } else {
                text1.setTextColor(Color.BLACK);
            }
            if (radioButton_three2.isChecked()) {
                text2.setTextColor(new Color(0xFFFFB90F));
            } else {
                text2.setTextColor(Color.BLACK);
            }
            if (radioButton_three3.isChecked()) {
                text3.setTextColor(new Color(0xFFFFB90F));
            } else {
                text3.setTextColor(Color.BLACK);
            }
            if (radioButton_three4.isChecked()) {
                radioButton_three4.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_email_yellow_24dp));
                text4.setTextColor(new Color(0xFFFFB90F));
            } else {
                radioButton_three4.setBackground(radiobutton_element_message);
                text4.setTextColor(Color.BLACK);
            }
            if (radioButton_three5.isChecked()) {
                text5.setTextColor(new Color(0xFFFFB90F));
            } else {
                text5.setTextColor(Color.BLACK);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        if (component.getId() == Id_back_button) {
            onBackPressed();
        } else if (component.getId() == Id_image1) {

        } else if (component.getId() == Id_image2) {

        }
    }
}
